
package com.be.beadmin.service.repository;

import com.be.beadmin.service.dto.input.NotifyInfoTotalByPage;
import com.be.beadmin.service.dto.output.NotifyInfoTotalEx;
import com.be.beadmin.service.entity.NotifyInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @author ljh
* @date 2023-09-20
**/
public interface NotifyInfoRepository extends JpaRepository<NotifyInfo, String>, JpaSpecificationExecutor<NotifyInfo> {

    NotifyInfo findNotifyInfoByIdAndDelete(String id, Integer delete);

    NotifyInfo findNotifyInfoById(String id);

    List<NotifyInfo> findByIdIn(List<String> ids);

    /**
     * @param pageEntity    分页参数
     * @param receiverName  接收人name
     * @param NotifyType   消息类型
     * @return  分页结果
     */

//    @Query(value = " select new com.be.beadmin.service.dto.output.NotifyInfoTotalEx (ni.module_name, ni.func_name, ni.notify_type, ni.notify_desc, ni.send_time, nt.status, nt.sender_id, nt.sender_name, nt.receiver_id, nt.receiver_name) "+
//             " from sys_notify_info ni inner join sys_notify_template_info nt on ni.id = nt.notify_info_id "+
//             " where nt.receiver_name = :receiverName and ni.notify_type = :NotifyType ")
//
//    Page<NotifyInfoTotalEx> getNotifyInfoTotalByPage(@Param("receiverName") String receiverName, @Param("NotifyType") String NotifyType, Pageable pageable);

    @Query(value = "SELECT new com.be.beadmin.service.dto.output.NotifyInfoTotalEx (nt.id, ni.id, ni.moduleName, ni.funcName, ni.notifyType, ni.notifyDesc, ni.sendTime, nt.status, nt.senderId, nt.senderName, nt.receiverId, nt.receiverName) " +
            "FROM NotifyTemplateInfo nt " +
            "INNER JOIN NotifyInfo ni ON ni.id = nt.notifyInfoId " +
            "WHERE ni.delete = 0 AND nt.delete = 0 " +
            "AND nt.receiverName = :#{#query.userName} AND ni.notifyType = :#{#query.notifyType} " +
            "AND (:#{#query.status} IS NULL OR nt.status = :#{#query.status}) " +
            "AND (:#{#query.notifyDesc} IS NULL OR ni.notifyDesc LIKE %:#{#query.notifyDesc}%) ")
    Page<NotifyInfoTotalEx> getNotifyInfoTotalByPage(@Param("query") NotifyInfoTotalByPage notifyInfoTotalByPage, Pageable pageable);
}